package com.kelvin.paopao.widgets.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by mc on 2017/3/8.
 * 支付聊天dialog
 */

public class SetPSMoneyDialogFragment extends DialogFragment {

    @BindView(R.id.et_send_money)
    EditText etSendMoney;
    private Activity activity;

    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);
        this.activity = activity;
    }

    public static SetPSMoneyDialogFragment newInstance() {
        SetPSMoneyDialogFragment f = new SetPSMoneyDialogFragment();

        Bundle args = new Bundle();
        f.setArguments(args);

        return f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {


        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.animate_dialog);

        View view = inflater.inflate(R.layout.dialog_set_ps_money, container);
        ButterKnife.bind(this, view);

        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.y = -100;
        getDialog().getWindow().setAttributes(params);

        etSendMoney.requestFocus();
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                String price = etSendMoney.getText().toString();
                if (TextUtils.isEmpty(price)) {
                    ToastUtil.showToast(getContext(), "请输入配送价格");
                    return;
                }
                RetrofitService.setPsPrice(price).subscribe(new Consumer<CommonListResponse>() {
                    @Override
                    public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                        if (commonListResponse.getReason().equals("成功")) {
                            ToastUtil.showToast(getContext(), "设置成功");
                            if (listener != null) {
                                listener.onSetComplete();
                            }
                            dismiss();
                        } else {
                            ToastUtil.showToast(getContext(), commonListResponse.getReason());
                        }
                    }
                });
                break;
        }
    }


    private OnSetCompleteListener listener;

    public void setListener(OnSetCompleteListener listener) {
        this.listener = listener;
    }

    public interface OnSetCompleteListener {
        void onSetComplete();
    }
}
